
package com.lc.dubbox.config;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.AnnotationBean;
import com.alibaba.dubbo.rpc.Exporter;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * 采用javaconfig配置方式(不用dubbox-provider.xml时)
 */
@Configuration
@PropertySource("classpath:dubbo.properties")
@Data
@ConditionalOnClass(Exporter.class)
public class DubboConfig {
    @Value("${dubbo.application.name}")
    private String applicationName;

    @Value("${dubbo.registry.protocol}")
    private String protocol;

    @Value("${dubbo.registry.address}")
    private String registryAddress;

    @Value("${dubbo.protocol.name}")
    private String name;

    @Value("${dubbo.protocol.port}")
    private Integer port;

    @Value("${dubbo.provider.timeout}")
    private int timeout;

    @Value("${dubbo.provider.retries}")
    private int retries;


    /**
     * @methodname annotationBean 的描述：设置dubbo扫描包
     * @param packageName
     * @return com.alibaba.dubbo.config.spring.AnnotationBean
     * @author
     * @create
     */

    @Bean
    public static AnnotationBean annotationBean(@Value("${dubbo.annotation.package}") String packageName) {
        AnnotationBean annotationBean = new AnnotationBean();
        annotationBean.setPackage(packageName);
        return annotationBean;
    }


    /**
     * @methodname applicationConfig 的描述：注入dubbo上下文
     * @param
     * @return com.alibaba.dubbo.config.ApplicationConfig
     */

    @Bean
    public ApplicationConfig applicationConfig() {
        // 当前应用配置
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(this.applicationName);
        applicationConfig.setLogger("slf4j");
        return applicationConfig;
    }

    /**
     * @methodname registryConfig 的描述：注入dubbo注册中心配置,基于zookeeper
     * @param
     * @return com.alibaba.dubbo.config.RegistryConfig
     */

    @Bean
    public RegistryConfig registryConfig() {
        // 连接注册中心配置
        RegistryConfig registry = new RegistryConfig();
        registry.setProtocol(protocol);
        registry.setAddress(registryAddress);
        registry.setCheck(false);// 设置启动时不检查注册中心
        return registry;
    }

    @Bean
    public ProtocolConfig protocolConfig() {
        //<dubbo:protocol name="rest" port="8888" threads="500" contextpath="api" server="tomcat" accepts="500"
        //extension="com.alibaba.dubbo.rpc.protocol.rest.support.LoggingFilter"/>
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setName(name);
        protocolConfig.setPort(port);
        protocolConfig.setThreads(500);
        protocolConfig.setContextpath("api");
        protocolConfig.setServer("tomcat");
        protocolConfig.setAccepts(500);
        protocolConfig.setExtension("com.alibaba.dubbo.rpc.protocol.rest.support.LoggingFilter");
        return protocolConfig;
    }


    /**
     * dubbo服务提供
     *
     * @param applicationConfig
     * @param registryConfig
     * @param protocolConfig
     * @return
     */
    @Bean(name="victory-springboot-dubbox-provider")
    public ProviderConfig providerConfig(ApplicationConfig applicationConfig, RegistryConfig registryConfig,
                                         ProtocolConfig protocolConfig) {
        ProviderConfig providerConfig = new ProviderConfig();
        providerConfig.setTimeout(timeout);
        providerConfig.setRetries(retries);
        //providerConfig.setDelay(delay);
        providerConfig.setApplication(applicationConfig);
        providerConfig.setRegistry(registryConfig);
        providerConfig.setProtocol(protocolConfig);
        return providerConfig;
    }

}

